package com.dwjf.core.net;

import android.content.Context;

import com.dwjf.core.net.callbak.Error;
import com.dwjf.core.net.callbak.Failure;
import com.dwjf.core.net.callbak.Request;
import com.dwjf.core.net.callbak.Success;
import com.dwjf.core.ui.LoaderStyle;

import java.util.Map;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class RestClientBulider {

    private String mUrl;
    private Map<String, Object> mParams = RestCreator.params;
    private Request mRequest;
    private Success mSuccess;
    private Error mError;
    private Failure mFailure;
    private RequestBody mBody;

    private LoaderStyle loaderStyle;
    private Context context;

    RestClientBulider(){}

    public  final RestClientBulider url(String url){
        this.mUrl = url;
        return this;
    }

    public  final RestClientBulider params(WeakHashMap<String, Object> params){
        this.mParams.putAll(params);
        return this;
    }

    public  final RestClientBulider params(String key, Object value){
        if (mParams == null){
            mParams = new WeakHashMap<>();
        }
        mParams.put(key, value);
        return this;
    }
    public  final RestClientBulider request(Request request){
        this.mRequest = request;
        return this;
    }
    public  final RestClientBulider success(Success success){
        this.mSuccess = success;
        return this;
    }
    public  final RestClientBulider error(Error error){
        this.mError = error;
        return this;
    }
    public  final RestClientBulider failuer(Failure failure){
        this.mFailure = failure;
        return this;
    }
    public  final RestClientBulider body(RequestBody body){
        this.mBody = body;
        return this;
    }

    public  final RestClientBulider loader(Context context, LoaderStyle loaderStyle){
        this.loaderStyle = loaderStyle;
        this.context = context;
        return this;
    }

    public  final RestClientBulider loader(Context context){
        this.loaderStyle = loaderStyle.BallClipRotatePulseIndicator;
        this.context = context;
        return this;
    }


    public  final RestClientBulider raw(String raw){
        this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),raw);
        return this;
    }
    public RestClient build(){
        return new RestClient(mUrl, mParams,
                mRequest, mSuccess,
                mError, mFailure, mBody,
                loaderStyle, context);
    }

}
